package com.example.demo.controller;

import com.example.demo.dto.ResponseResult;
import com.example.demo.entity.Relics;
import com.example.demo.service.RelicsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/app/relics")
public class RelicsController {

    @Autowired
    private RelicsService relicService;

    //模糊搜索
    @GetMapping("/get/search")
    public ResponseResult<List<Relics>> searchRelics(@RequestParam String keyword) {
        try {
            List<Relics> result = relicService.searchRelics(keyword);
            return new ResponseResult<>(200, "success", result);
        } catch (Exception e) {
            return new ResponseResult<>(500, "搜索失败: " + e.getMessage(), Collections.emptyList());
        }
    }
    // 获取所有文物列表
    @GetMapping("/get/list")
    public ResponseResult<List<Relics>> getAllRelics() {
        System.out.println("dfiaoyong");
        try {
            List<Relics> relics = relicService.getAllRelics();
            return new ResponseResult<>(200, "success", relics);
        } catch (Exception e) {
            return new ResponseResult<>(500, "获取列表失败: " + e.getMessage(), Collections.emptyList());
        }
    }
    // 获取文物详情
    @GetMapping("/get/{id}")
    public ResponseResult<Relics> getRelicDetail(@PathVariable int id) {
        System.out.println("迪迦");
        try {
            Relics relic = relicService.getRelicDetail(id);
            if (relic == null) {
                return new ResponseResult<>(404, "文物不存在", null);
            }
            return new ResponseResult<>(200, "success", relic);
        } catch (Exception e) {
            return new ResponseResult<>(500, "获取详情失败: " + e.getMessage(), null);
        }
    }

}
